extends Node
#在第一次打开MOD设置或者点击开始游戏时，将此节点注入到sys

#存放所有mod设置的字典
var settings = {}
#存放背景图片
var bgPics = []
#charaV地址，动态角色charaV会加载该地址
var charaV = preload("res://!ve/base/tscn/chara/charaV.tscn")

onready var utils = load(data.infoDs["VEUtils"]["VEUtils"].dir).new()
onready var file = File.new()
onready var trait = load(data.infoDs["VEBaseTrait"]["VEBaseTrait"].dir).new()
# onready var oriMapSceneScript = load("res://tscn/map/mapScene.gd")
onready var mapSceneScript = null
func _ready():
	print("VEbBase ready")
	replaceBaseNode()
	reInitDataSkills()
	#注册快捷键
	registerKeyMap()
	#加载所有MOD设置
	loadSettings()
	pause_mode = Node.PAUSE_MODE_PROCESS
	execTopNodes()
	sys.get_tree().connect("node_added", self, "on_node_added")
	add_child(trait)
	
#重新初始化data.skills，以注入mod修改技能
func reInitDataSkills():
	data.skills.clear()
	for i in data.getList("k"):
		var item = data.newBase(i.id) 
		if data.isUsable(item) : 
			data.skills.addItem(item)	

#替换一些原版的基础节点
func replaceBaseNode():
	utils.changeScript(cons, load("res://!ve/base/tscn/cons.gd"))
	cons.init()

func registerKeyMap():
	InputMap.add_action("ui_f1")
	var ev1 = InputEventKey.new()
	ev1.pressed = true
	ev1.scancode = KEY_F1  
	InputMap.action_add_event("ui_f1", ev1)
#

func _input(event):
	if event.is_action_pressed("ui_f1") and sys.game != null:	
		sys.newDlg("res://tscn/game/testDlg.tscn")

func on_node_added(node):
	if node.name == 'charaDlg':
		yield(get_tree().create_timer(0.01),"timeout")	
		var chara = node.cha
		if chara.get("dlg") != null:
			var newNode = newLoadedDlg(chara.dlg)
			newNode.init(chara)
			newNode.pause_mode = Node.PAUSE_MODE_PROCESS
			node.del()
	elif node is MapScene and mapSceneScript != null:
		#替换自定义mapScene
		utils.changeScript(node, mapSceneScript)

#在VEBase初始化后执行所有mod的top节点
func execTopNodes():
	# print(data.infoDs.keys())
	if not "topNode" in data.infoDs.keys(): 
		return
	for i in data.getList("topNode"):
		var ins = load(i.dir).new()
		if ins.has_method("init"):
			ins.init()

#获得MOD设置指定词条
#mod: mod别名，由modder自行指定，尽量特殊一些别冲突
#name: 该mod设置中的词条名称
#default: 若为null，返回默认值
func getSetting(mod, name, default = null):
	var setting = settings.get(mod)
	if setting == null:
		setting = {}
		settings[mod] = setting
	var s = setting.get(name)
	if s == null and default != null:
		setSetting(mod, name, default)
		s = default
	return s

#修改mod设置指定词条
#mod: mod别名，由modder自行指定，尽量特殊一些别冲突
#name: 该mod设置中的词条名称
#value: 该词条的值
#save: 是否立刻将该变动写入到本地文件中
func setSetting(mod, name, value, save = true):
	var setting = settings.get(mod)
	if setting == null:
		setting = {}
		settings[mod] = setting

	setting[name] = value
	if save:
		saveSetting(mod)

func saveSetting(key):
	file.open("user://modSetting/%s.save"%[key], File.WRITE)
	file.store_var(settings[key])
	file.close()

func saveSettings():
	for i in settings.keys():
		saveSetting(i)

func loadSettings():
	var dirStr = "user://modSetting"
	var dir = Directory.new()
	if not dir.file_exists(dirStr):
		dir.make_dir(dirStr)
	dir.open(dirStr)
	dir.list_dir_begin(true, true)
	var dname = dir.get_next()
	while dname != "":
		if dname.find(".save") > -1:
			var ext = dname.split(".save")[0]
			file.open(dirStr + "/" + dname, File.READ)
			settings[ext] = file.get_var()
			file.close()
		dname = dir.get_next()
	dir.list_dir_end()
	
func newLoadedDlg(file):
	var dlg = file.instance()
	sys.main.ui.add_child(dlg)
	dlg.popup()
	return dlg


